%!TEX root = report.tex

\section{Developing the Metamodel}
\label{sec:dsl}

In order to develop the metamodel of the textual DSL for specifying the modes of a bicycle computer, we followed a two-step process:

\begin{enumerate}
	\item \textbf{Identifying Requirements:} We extracted the requirements that our modelling language must meet from the problem definition given in Section \ref{sec:intro}. 
	\item \textbf{Specifying the Language Grammar:} We used the identified requirements to develop a metamodel for the DSL.
\end{enumerate}

Subsection \ref{subsec:modreq} describes the requirements of the DSL for specifying bicycle computer modes, subsection \ref{subsec:langgram} describes the metamodel of the DSL, and subsection \ref{subsec:editorfeat} describes the features of the DSL Editor generated by Xtext.

\subsection{Model Requirements}
\label{subsec:modreq}

The first step in the metamodel development process is elicitation of requirements from the problem definition. The following requirements were extracted:

\begin{enumerate}
	\item \textbf{Multiple Modes:} The user must be allowed to specify multiple modes in a convenient manner.
	\item \textbf{Mode Identifier:} Each mode must have a unique identifier string of upto 3 characters. This identifier will be used to identify the current mode on the bicycle computer display.
	\item \textbf{Mode Type: } Each mode must have a particular type depending on the nature of the value to be stored. We identified three types that are required for implementing the basic mode functions: \textit{integer}, \textit{real} and \textit{time}.
	\item \textbf{System Values:} There must be some system specified values available for use in mode functions. These include the number of rotations performed by the bicycle wheel in the last second and the circumference of the wheel.
	\item \textbf{Mode Expression:} The user must be able to specify the value of computed mode functions using complex mathematical expressions with addition, subtraction, multiplication and division operations. Bracketed expressions must be allowed to specify the order of operations. The expressions can be constructed using values of other mode functions, built in system values and numeric literals (integers and real values).
	\commentout{ %this is a dublicate
	\item \textbf{Mode Expression:} The user must be able to specify the value of computed mode functions using complex mathematical expressions with addition, subtraction, multiplication and division operations. Bracketed expressions must be allowed to specify the order of operations. The expressions can be constructed using values of other mode functions, built in system values and numeric literals (integers and real values).
	}
	\item \textbf{Mode Condition:} There are some mode values (such as maximum speed) that are only updated if a particular condition is met. For this reason, conditions must be supported in our DSL. The conditions can be specified using existing mode functions, system values and numeric literals. Three comparison operators should be supported: greater than, less than and equal to. There may also be compound conditions, i.e., multiple conditions joined by AND or OR constructs.
	\item \textbf{Invisible Modes:} Some modes may simply be intermediate modes that are used in calculation of other modes but should not be included in the displayed mode functions on the bicycle computer.	
\end{enumerate}


\subsection{Language Grammar}
\label{subsec:langgram}

The second step in the process of metamodel development is specification of the language grammar.  For this step, we designed a mode function template for our DSL and then implemented it using Xtext. 

\textbf{Mode Function Template:} The mode function template is given below:

\begin{verbatim}
MODE <mode-identifier> [INVISIBLE]
TYPE <type>
[CONDITION <condition>]
EXPRESSION <expression>
\end{verbatim}

\text MODE, INVISIBLE, TYPE, CONDITION, and EXPRESSION are reserved keywords in the bicycle computer DSL. In order to write a mode function the following must be specified:

\begin{enumerate}
\item \textit{mode-identifier}: A string of up to 3 characters used to identify the mode on the bicycle computer display, and for use in other mode functions.
\item \textit{type}: One of the three types supported by our DSL: \textit{integer}, \textit{real} and \textit{time}.
\item \textit{expression}: A mathematical expression for updating the value of the mode. The expression can involve other mode functions, system values such as \textit{rot}(rotations of wheel in last second) and \textit{cir} (circumference of wheel), and numeric literals.
\end{enumerate}

Optionally, the user may also specify:

\begin{enumerate}
\item \textit{INVISIBLE}: The user may specify if the mode is invisible by including the keyword INVISIBLE after the \textit{mode-identifier}. By default, all modes are visible.
\item \textit{condition}: The user may specify a condition for updating the mode value by including the keyword CONDITION followed by a boolean expression using comparison operators and the boolean connectives AND and OR.
\end{enumerate}

\textbf{Xtext Implementation:} Implementing the mode function template in Xtext was relatively straight-forward. As Xtext requires that the language grammar be defined in Extended Backus-Naur Form (EBNF) and that the grammar must be left-factored, our knowledge of compiler construction was particularly useful. A simplified version of the grammar (not left-factored for ease of understanding) we implemented is given below:

\begin{verbatim}
Model = Modes*;
Mode = `MODE' MNAME (`INVISIBLE')? 
    `TYPE' TYPE 
    (`CONDITION' Condition)? 
    `EXPRESSION' Expression;
Condition = (Condition (`AND' | `OR') Condition) | 
    (Expression (`>' | `<' | `=') Expression);
Expression = (Expression 
    (`+' | `-' | `*' | `\' | `%') Expression) | 
    `(' Expression `)' | PrimaryExpression;
PrimaryExpression = NUMBER | Mode->MNAME | SYS_VAL;

MNAME = (`a'..`z')((`a'..`z')(`a'..`z')?)?;
NUMBER = (`0'..`9')* (`.' (`0'..`9')+)?;
SYS_VAL = `rot' | `cir';
TYPES = `time' | `integer' | `real';
\end{verbatim}

The actual source code for the language grammar is given in Figure \ref{fig:gram_src}.
\begin{figure*}
	\includegraphics[width=2.0\columnwidth]{imgs/gram_src.png}
	\caption{Xtext language grammar source code.}
	\label{fig:gram_src}
\end{figure*} 


\subsection{DSL Editor}
\label{subsec:editorfeat}

Once the DSL grammar is implemented in Xtext, it is compiled into an Eclipse Plugin. Xtext takes the grammar definition as input and creates a plugin that provides syntax highlighting, code completion, validation and an outline view for the specified DSL. A new instance of Eclipse can be launched with the aforementioned plugin integrated in it. This instance can be used to create textual models in our DSL. A snapshot of the DSL editor is given in Figure \ref{fig:dsl_editor}.

\begin{figure*}
	\includegraphics[width=2.0\columnwidth]{imgs/dsl_editor.png}
	\caption{DSL Editor generated by Xtext.}
	\label{fig:dsl_editor}
\end{figure*} 



